﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace NB.MGJ.Common
{public  class Tts_Service
    {

        public  string Tts(string filename, string ttscontent, string vloname)
        {
            string _result = string.Empty;
            IPAddress ip = IPAddress.Parse("101.37.205.87");//需修改
            IPEndPoint ipEnd = new IPEndPoint(ip, 8501);//需修改
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            try
            {
                socket.Connect(ipEnd);
            }
            catch
            {
                return null;
            }

            socket.Send(Encoding.UTF8.GetBytes(string.Format("{0}&voice_name ={1},speed = 50,volume = 50&{2}&", ttscontent,vloname,filename)));//发送数据
            Stopwatch sw = new Stopwatch();//启用定时器
            sw.Start();
            while (true && sw.ElapsedMilliseconds < 10000)//用于接收返回数据
            {
                byte[] data = new byte[1024];
                socket.Receive(data);
                string stringData = Encoding.UTF8.GetString(data).Replace("\0","");
                if (!string.IsNullOrWhiteSpace(stringData))
                {
                    _result = stringData;
                    break;
                }
            }
            sw.Reset();
            socket.Shutdown(SocketShutdown.Both);
            socket.Close();
            return _result;
        }
    }
}
